﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Csla;
    using Csla.Data;
    using Csla.Reflection;
    using Csla.Server;

    using Common = MarketPulse.BusinessLogic.Common;
    using MarketPulse.BusinessLogic.Common.Criteria;

    using MarketPulse.DataAccess.DataContextObjects.Production;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    /// <summary>
    /// Abstracts the data access for a PaymentType away from a Payment Business Object.
    /// </summary>
    public class PaymentTypeFactory : ObjectFactory
    {
        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        #region Methods

            #region Fetch Methods

                #region PaymentTypeInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only PaymentType objects from the database
                    /// </summary>
                    /// <returns>A read only list of read only PaymentType objects retrieved from the database</returns>
                    public Common.PaymentTypeInfoList FetchInfoList()
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var paymentTypeInfoList = (Common.PaymentTypeInfoList)MethodCaller.CreateInstance(typeof(Common.PaymentTypeInfoList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspPaymentTypeSelectAll();

                                // PaymentTypeInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(paymentTypeInfoList, false);
                                foreach (UspPaymentTypeSelectAllResult result in selectResult)
                                {
                                    Common.PaymentTypeInfo paymentTypeInfo = (Common.PaymentTypeInfo)MethodCaller.CreateInstance(typeof(Common.PaymentTypeInfo));

                                    // Common properties inherited from MarketPulseBase
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.GUIDProperty, result.PaymentTypeGUID);
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties
                                    LoadProperty(paymentTypeInfo, Common.PaymentTypeInfo.mPaymentTypeLabelProperty, result.PaymentTypeName);
                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(paymentTypeInfo);

                                    // Add the item to the list
                                    paymentTypeInfoList.Add(paymentTypeInfo);
                                }
                                SetIsReadOnly(paymentTypeInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(paymentTypeInfoList);

                                return paymentTypeInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

            #endregion

        #endregion
    }
}
